layui.use(['layer','form','table','element','layedit','upload'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element
        ,layedit = layui.layedit
        ,upload = layui.upload;

    var projectId = $("#id").val();


    //初始化富文本编辑器
    var index = layedit.build('addEdit1', {
        tool: [
            'strong', 'italic', 'underline', 'del',
            ,'addhr' //添加水平线
            ,'|', 'fontFomatt','fontfamily','fontSize', //段落格式，字体样式，字体颜色
            , 'colorpicker', 'fontBackColor'//字体颜色，字体背景色
            , 'face', '|', 'left', 'center', 'right', '|', 'link', 'unlink'
            ,'image'//原版上传图片
            ,'images'//多图上传
            , 'image_alt'//上传图片拓展
            ,'anchors' //锚点
            , '|', 'table'//插入表格
            ,'customlink'//插入自定义链接
            ,'fullScreen'//全屏
        ]
        , height: 600
        ,   uploadImage: {
            url: '/admin/business/uploadBusinessImg'
            ,type: 'post' //默认post
        }
    });


    //表单验证
    form.verify({
        id:function (value) {
            if(value == ""){
                return "id不能为空";
            }
        },
        content:function () {
            var content = layedit.getContent(index);
            if(content == "" || content.length==0){
                return "内容不能为空";
            }
        }
    });

    //提交表单
    form.on('submit(form)',function (data) {

        layer.msg('修改中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });
        //校验类型和标签是否选择
        // var label_length = $("input[name=label]:checked").length;
        // var type_length = $("input[name=type]:checked").length;

        var paramData = {
            "id":$("#id").val(),
            "content":layedit.getContent(index)
        };

        var data2 = JSON.stringify(paramData);

        //保存项目
        $.ajax({
            url:"/admin/business/update",
            type:"POST",
            data:data2,
            // dataType:"json",
            contentType:"application/json;charset=utf-8",
            success:function (result) {
                // console.log("data=="+form_data)
                if(result.flag){
                    layer.msg(result.message);
                    //清空内容
                    // $("#form").reset();
                    document.getElementById("form").reset();
                    layui.form.render();
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统错误！");
            }
        });

    });


});
